﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Utils;
using Telerik.WebControls;

public partial class sysadmin_CourseManager_AddCourseInfo :PowerCreator.LMS.Web.Base.WebBasePage
{
    Course opCourse = new Course();
    VirtualDirectory opVirtual = new VirtualDirectory();
    Catalog opCatalog = new Catalog();
    PowerCreator.LMS.Web.DL.Project.TOAdvancedUser objAdvanceUser = new TOAdvancedUser();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        if (!IsPostBack)
        {
            string catalogid = "0";
            InitPage();

            BinderCatalog();
            BinderVirtual();
            BinderXueduan();
            BinderTeacher();
            BinderXueNian();
            BindTrainType();

            if (Request.QueryString["CatalogID"] != null)
            {
                catalogid = Request.QueryString["CatalogID"].ToString();
                ViewState["CatalogID"] = catalogid;
                ddl_Catalog.SelectedValue = catalogid;
            }
            if (Request.QueryString["AddCourseType"] != null)
            {
                if(Request.QueryString["AddCourseType"].ToString()=="uploadPic")
                   BinderForm();
            }
            ViewState["CatalogID"] = catalogid;
        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 2)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }

        }


    }

    private void BinderCatalog()
    {
        string search = "";
        DataTable dt = opCatalog.QueryCatalog(search);
        ArrayList list = opCatalog.GetCatalogTree(dt);
        ddl_Catalog.DataSource = list;
        ddl_Catalog.DataTextField = "Text";
        ddl_Catalog.DataValueField = "Value";
        ddl_Catalog.DataBind();
    }
    private void BinderXueduan()
    {
        TOSchoolStage obj = new TOSchoolStage();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_xueduan.DataSource = dt;
        ddl_xueduan.DataTextField = "SchoolStage";
        ddl_xueduan.DataValueField = "ID";
        ddl_xueduan.DataBind();

    }
    private void BinderVirtual()
    {
        string search = "";
        DataTable dt = opVirtual.QueryVirtual(search);
        ddlVirtualDirectory.DataSource = dt;
        ddlVirtualDirectory.DataTextField = "VirIisName";
        ddlVirtualDirectory.DataValueField = "VirId";
        ddlVirtualDirectory.DataBind();
    }
    private void BinderTeacher()
    {
        string search = "";
        DataTable dt = objAdvanceUser.Query(search);
        ddl_CourseMostlyTeacher.DataSource = dt;
        ddl_CourseMostlyTeacher.DataTextField = "Name";
        ddl_CourseMostlyTeacher.DataValueField = "AUser_Id";
        ddl_CourseMostlyTeacher.DataBind();
    }
    private void BinderXueNian()
    {
        TOXuenian obj = new TOXuenian();
        string search = "";
        DataTable dt = obj.Query(search);
        ddl_xuenian.DataSource = dt;
        ddl_xuenian.DataTextField = "XueNian";
        ddl_xuenian.DataValueField = "ID";
        ddl_xuenian.DataBind();
    }
    private void BinderForm()
    {
        txt_CourseName.Text = Request.QueryString["coursename"].ToString();
        txt_pic.Text = Request.QueryString["picpath"].ToString();
        txt_pic.ReadOnly = true;
    }

    /// <summary>
    /// 绑定培训类型
    /// </summary>
    private void BindTrainType()
    {
        TOTrainType obj = new TOTrainType();

        DataTable dt = obj.Query("");
        chk_peixunleixing.DataSource = dt;
        chk_peixunleixing.DataTextField = "ShortName";
        chk_peixunleixing.DataValueField = "ID";
        chk_peixunleixing.DataBind();
    }



    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddCourseInfo_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseName.Text = GetLanageValueByKey("lbl_AddCourseInfo_CourseName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseType.Text = GetLanageValueByKey("lbl_AddCourseInfo_CourseType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherName.Text = GetLanageValueByKey("lbl_AddCourseInfo_TeacherName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseCode.Text = GetLanageValueByKey("lbl_AddCourseInfo_CourseCode", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Type.Text = GetLanageValueByKey("lbl_AddCourseInfo_Type", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_CourseAllowSelect.Text = GetLanageValueByKey("lbl_AddCourseInfo_CourseAllowSelect", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CoursePeriod.Text = GetLanageValueByKey("lbl_AddCourseInfo_CoursePeriod", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TextBox.Text = GetLanageValueByKey("lbl_AddCourseInfo_TextBox", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_VirtualDirectoryinfo.Text = GetLanageValueByKey("lbl_AddCourseInfo_VirtualDirectoryinfo", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_VirtualDirectory.Text = GetLanageValueByKey("lbl_AddCourseInfo_VirtualDirectory", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Stateinfo.Text = GetLanageValueByKey("lbl_AddCourseInfo_Stateinfo", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_State.Text = GetLanageValueByKey("lbl_AddCourseInfo_State", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_msg_description.Text = GetLanageValueByKey("AddCourseInfo_lbl_l1", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_CourseIsRecommend.Text = GetLanageValueByKey("AddCourseInfo_lbl_l2", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseIsRecommendtxt.Text = GetLanageValueByKey("AddCourseInfo_lbl_l3", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseImgUrl.Text = GetLanageValueByKey("AddCourseInfo_lbl_l4", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_UploadFile.Text = GetLanageValueByKey("AddCourseInfo_lbl_l5", PowerCreator.LMS.Language.LanagetItemType.Page);
        //rdbtnBiXiu.Text = GetLanageValueByKey("Course_db_d1", PowerCreator.LMS.Language.LanagetItemType.Page);
        //rdbtnXuanXiu.Text = GetLanageValueByKey("Course_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);
        //rdbtnGongKai.Text = GetLanageValueByKey("Course_db_d3", PowerCreator.LMS.Language.LanagetItemType.Page);
        //rbtn_Yes.Text = GetLanageValueByKey("Course_db_d4", PowerCreator.LMS.Language.LanagetItemType.Page);
        //rbtn_No.Text = GetLanageValueByKey("Course_db_d5", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_CourseName.ErrorMessage = GetLanageValueByKey("AddCourseInfo_msg_m1", PowerCreator.LMS.Language.LanagetItemType.Page);

        rdbtn_stateOK.Text = GetLanageValueByKey("AddCourseInfo_db_d1", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_stateNO.Text = GetLanageValueByKey("AddCourseInfo_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Save.Text = GetLanageValueByKey("btn_AddCourseInfo_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_AddCourseInfo_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    
    private bool CheckPageData()
    {
        if (!IntUtils.IsNumber(txt_CoursePeriod.Text))
        {
            lbl_addcourseinfo_msg_courseperiod.Text = GetLanageValueByKey("lbl_addcourseinfo_msg_courseperiod", PowerCreator.LMS.Language.LanagetItemType.MSG);
            return false;
        }
        if (string.IsNullOrEmpty(ddlVirtualDirectory.SelectedValue))
        {
            lbl_addcourseinfo_msg_virtual.Text = GetLanageValueByKey("lbl_addcourseinfo_msg_virtual", PowerCreator.LMS.Language.LanagetItemType.MSG);
            return false;
        }
        if (opCourse.QueryCourseCount("CourseName='" + txt_CourseName.Text.Trim() + "'") > 0)
        {
            lab_CouseMsg.Text = GetLanageValueByKey("AddCourseInfo_dbname_msg_m1", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        return true;
    }


 



    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "CourseInfoManager.aspx";
        Script.RedirectURL(url);
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;

        VOTOCourse vo = new VOTOCourse();

        vo.CourseID = "1";// txt_CourseID.Text.Trim();
        vo.CourseCode = txt_CourseCode.Text;
        vo.Description = FreeTextBox1.Text;
        vo.CourseImgUrl = txt_pic.Text.Trim();
        vo.CourseIsCompulsory = 1;
        if (cb_CourseIsRecommend.Checked == true)
            vo.CourseIsRecommend = 1;
        else
            vo.CourseIsRecommend = 0;
        if (rdbtnXuanXiu.Checked)
            vo.CourseIsCompulsory = 2;
        //else if (rdbtnGongKai.Checked)
        //    vo.CourseIsCompulsory = 3;
        vo.CourseIsRecommend = 0;
        vo.CourseLastUpdateTime = DateTime.Now;
        vo.FKCourseMostlyTeacherID = int.Parse(ddl_CourseMostlyTeacher.SelectedValue);
        vo.CourseMostlyTeacherName = ddl_CourseMostlyTeacher.SelectedItem.Text;
        vo.CourseName = txt_CourseName.Text;

        vo.CoursePeriod = int.Parse(txt_CoursePeriod.Text);
        vo.Score = int.Parse(txt_xuefen.Text.Trim());
        vo.XueNian = int.Parse(ddl_xuenian.SelectedValue);
        vo.TrainFormat = int.Parse(ddl_peixunxingshi.SelectedValue);
        vo.TrainType = getTrainTypeIDlist();
        vo.CourseResourceNum = 0;
        vo.CourseState = 0;
        if (rdbtn_stateOK.Checked)
            vo.CourseState = 1;
        else if (rdbtn_stateNO.Checked)
            vo.CourseState = 0;
        if (rdbtn_Yes.Checked)
            vo.Used = true;
        else if (rdbtn_No.Checked)
            vo.Used = false;

        PowerCreator.LMS.Web.Base.BasePage bpage = new PowerCreator.LMS.Web.Base.BasePage();
        vo.FKCourseCreatorid = bpage.GetCreatedByInfo().UserID;
        
        vo.FKCourseVirId = int.Parse(ddlVirtualDirectory.SelectedValue.ToString());
        vo.KeyID = "";
        vo.CourseCreatetime = DateTime.Now;
        vo.CourseCreatoname = bpage.GetCreatedByInfo().UserName;

        ViewState["CatalogID"] = ddl_Catalog.SelectedValue.ToString();
        int catalogid = int.Parse(ViewState["CatalogID"].ToString());
        vo.Xueke = int.Parse(ViewState["CatalogID"].ToString());
        vo.Xueduan = int.Parse(ddl_xueduan.SelectedValue);

        if (opCourse.AddCourse(vo,catalogid,bpage.GetCreatedByInfo()))
        {
            string url = "Course.aspx";
            Script.RedirectURL(url);
        }
    }
    private string getTrainTypeIDlist()
    {
        string trainTypeIDlist = "-1,";

        for (int j = 0; j < chk_peixunleixing.Items.Count; j++)
        {
            if (chk_peixunleixing.Items[j].Selected)
            {
                trainTypeIDlist += chk_peixunleixing.Items[j].Value + ",";
            }
        }
        trainTypeIDlist += "0";
        if (trainTypeIDlist == "-1,0")
            trainTypeIDlist = "0";
        return trainTypeIDlist;
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "Course.aspx";
        Script.RedirectURL(url);
    }
    
    protected void btnBack_Click(object sender, EventArgs e)
    {
       // btn_Back.Enabled = false;
        foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
        {
            string Url = "";
            Url = WebUtils.WebRoot + "/fileManager/UploadFile/CoursePic/";
            string Path = Server.MapPath(Url);

            //如果路径不存在，则创建
            if (System.IO.Directory.Exists(Path) == false)
            {
                System.IO.Directory.CreateDirectory(Path);
            }

            string filetype = file.GetExtension();
            if (!IsUploadFileType(filetype)) return;

            //组合路径，file.GetName()取得文件名
            string fileName = opCourse.GetUploadResourceFileName(file.GetExtension());

            Path = Path + fileName;
            Url = Url + fileName;
            //保存
            file.SaveAs(Path, true);
            txt_pic.Text = Url;
            btn_Back.Enabled = true;
        }
    }

    //检查上传文件类型
    public bool IsUploadFileType(string extension)
    {
        string allowExtensions = "gif|jpg";    //允许上传的类型
        if (!opCourse.IsAllowedExtension(extension, allowExtensions))
        {
            lbl_FileUpload_msg_FileType.Text = GetLanageValueByKey("AddCourseInfo_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page) + allowExtensions;
            return false;
        }
        return true;
    }
}
